<!--
 * @Description:厂端车主关键信息查询
 * @Autor: 刘诗文
 * @Date: 2020-12-23 15:28:42
 * @LastEditors: 马毅
 * @LastEditTime: 2021-04-15 17:17:25
-->
<template>
  <div>
    <TopFilter :list="topFilterList" :cols="4" :labelWidth="105" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <FilterTable
      ref="table"
      selectionType="single"
      :isSelectColumn="false"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'rows'"
      columnsRef="keyInfoChangeOfOwnerQuerylsw"
      :onColumnsChange="columns => (this.columns = columns)"
      :onSyncTableData="tableChangeFun"
    >
      <template slot="controls">
        <AjaxButton size="small" type="primary" icon="el-icon-download" :click="ownerExportData">导出</AjaxButton>
      </template>
    </FilterTable>
    <BaseDialog :visible.sync="actions.visible" :title="actions.title" width="300" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <searchDearlerList :resData="this.resData" @dealerData="dealerData" @close="closeSetting" />
    </BaseDialog>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { getOwnerExcelList, getProvince, getCitys, getDistrict, getRegion } from '@/api/customerRepair/customerInfo/ownerVehicleManagement';
import searchDearlerList from '@/pages/customerRepair/vehicleOwnerManagement/vehicleChangeQuery/searchDearlerList';
import { getKeyInfoChangeList, getDearlerListByTranslate, getKeyInfoChangeExport } from '@/api/customerRepair/vehicleOwnerManagement/vehicleChange';
import { notifyAction, isFormEmpty, findTableItem } from '@/utils';
import { getAllUser } from '@/api/customerRepair/afterSaleVisit/resultEntry';
import { exportExcelFile } from '@/utils/index';
import { getNameByIds } from '@/api/repair/common';

export default {
  name: 'keyInfoChangeOfOwnerQuery',
  mixins: [dictionary],
  components: {
    searchDearlerList
  },
  data() {
    this.BaseTable = null;
    this.type = '';
    this.resData = {};
    this.title = '';
    // isHidden: 'none';
    return {
      topFilterList: this.createTopFilterList(),
      fetchApi: getKeyInfoChangeList,
      fetchParams: { pageNum: 1, xhrAbort: true, type: 'OEM' },
      columns: this.createTableColumns(),
      actions: { title: '', visible: false, type: '' },
      dealerList: [],
      regionList: []
    };
  },
  created() {
    // 获取全部人员
    // this.getAllUser();
  },
  mounted() {
    this.BaseTable = this.$refs.table;
    // this.menuUserAction();
    this.getProvine();
    this.getRegion();
    this.getDearlerListByTranslate();
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin'
        },
        {
          type: 'INPUT',
          label: '车主手机号',
          fieldName: 'mobile'
        },
        {
          type: 'INPUT',
          label: '车主姓名',
          fieldName: 'ownerName'
        },
        {
          type: 'INPUT',
          label: '经销商',
          fieldName: 'ownerCode',
          readOnly: true,
          initialValue: '',
          unitRender: props => {
            return <el-button icon="el-icon-search" onClick={this.queryDealer} />;
          }
        },
        // {
        //   type: 'INPUT',
        //   label: '新车主手机号',
        //   fieldName: 'mobile_new'
        // },
        {
          type: 'RANGE_DATE',
          label: '变更日期',
          style: { minWidth: '200px' },
          fieldName: 'changeTimeBegin|changeTimeEnd'
        },
        {
          type: 'SELECT',
          label: '审核结果',
          fieldName: 'status',
          itemList: this.createDictList(1261)
        },
        {
          type: 'SELECT',
          label: '所在省份',
          fieldName: 'province',
          itemList: [],
          change: val => {
            this.provinceChange(val);
            let itemList = this.formList.find(item => item.fieldName === 'province').itemList;
          }
        },
        {
          type: 'SELECT',
          label: '城市',
          fieldName: 'city',
          itemList: [],
          change: val => {
            this.cityChange(val);
            let itemList = this.formList.find(item => item.fieldName === 'city').itemList;
          }
        },
        {
          type: 'SELECT',
          label: '区县',
          fieldName: 'district',
          itemList: [],
          change: val => {
            let itemList = this.formList.find(item => item.fieldName === 'district').itemList;
          }
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '经销商代码',
          dataIndex: 'OWNER_CODE',
          showOverflowTooltip: true
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          showOverflowTooltip: true,
          render: props => {
            return <span>{this.getText('dealer', props.row.OWNER_CODE)}</span>;
          }
        },
        {
          title: '客户性质',
          dataIndex: 'ownerProperty',
          width: 150,
          showOverflowTooltip: true,
          render: props => {
            return <span>{this.createDictText(props.row.ownerProperty, 8011)}</span>;
          }
        },
        {
          title: '车主编号',
          dataIndex: 'ownerNo',
          width: 120,
          showOverflowTooltip: true
        },
        {
          title: '车主姓名',
          dataIndex: 'newOwnerName',
          width: 150,
          showOverflowTooltip: true
        },
        {
          title: '性别',
          dataIndex: 'gender',
          width: 150,
          showOverflowTooltip: true,
          render: props => {
            return <span>{this.createDictText(props.row.gender, 8012)}</span>;
          }
        },
        {
          title: '车主电话',
          dataIndex: 'phone',
          width: 100,
          showOverflowTooltip: true
        },
        {
          title: '车主手机',
          dataIndex: 'mobile',
          width: 100,
          showOverflowTooltip: true
        },
        {
          title: '证件类型',
          dataIndex: 'ctCode',
          width: 130,
          showOverflowTooltip: true,
          render: props => {
            return <span>{this.createDictText(props.row.ctCode, 8013)}</span>;
          }
        },
        {
          title: '证件号',
          dataIndex: 'certificateNo',
          width: 130,
          showOverflowTooltip: true
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          width: 130,
          showOverflowTooltip: true
        },
        {
          title: '省份',
          dataIndex: 'province',
          width: 130,
          showOverflowTooltip: true,
          render: props => {
            return <span>{this.getText('region', props.row.province)}</span>;
          }
        },
        {
          title: '城市',
          dataIndex: 'city',
          width: 130,
          showOverflowTooltip: true,
          render: props => {
            return <span>{this.getText('region', props.row.city)}</span>;
          }
        },
        {
          title: '操作人',
          dataIndex: 'operator',
          width: 120,
          showOverflowTooltip: true,
          dictItems: []
        },
        {
          title: '变更申请时间',
          dataIndex: 'changeTime',
          width: 150
        },
        {
          title: '审核时间',
          dataIndex: 'examDate',
          width: 150
        },
        {
          title: '审核结果',
          dataIndex: 'status',
          width: 90,
          showOverflowTooltip: true,

          render: props => {
            return <span>{this.createDictText(props.row.status, 1261)}</span>;
          }
        },
        {
          title: '审核信息',
          dataIndex: 'remarks',
          showOverflowTooltip: true
        }
      ];
    },
    changeHandle(val) {
      val.type = 'OEM';
      this.fetchParams = Object.assign({}, val, { xhrAbort: false });
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.$refs.table.$refs.pageTable.EXECUTE_RESET_HEIGHT();
      });
    },
    // 打开经销商页面
    queryDealer() {
      this.actions = { visible: true, title: '经销商查询', type: 'search' };
    },
    // 获取经销商数据
    dealerData(val) {
      this.topFilterList.find(x => x.fieldName === 'ownerCode').initialValue = val.dealerCode;
      this.closeSetting();
    },
    // 关闭经销商页面
    closeSetting() {
      this.actions.visible = false;
    },
    // 查询全部经销商列表
    async getDearlerListByTranslate() {
      const res = await getDearlerListByTranslate();
      if (res.resultCode === 200) {
        if (res.errMsg) {
          notifyAction(res.errMsg, 'error');
        } else {
          this.dealerList = res.data;
        }
      }
    },
    // 翻译table
    getText(type, val) {
      if (type === 'dealer') {
        return this.dealerList.find(x => x.dealerCode === val) ? this.dealerList.find(x => x.dealerCode === val).dealerName : '';
      } else if (type === 'region') {
        return this.regionList.find(x => x.value === val) ? this.regionList.find(x => x.value === val).text : '';
      }
    },
    // 清空下拉框表当控件的值和下拉列表
    clearFormItem(item) {
      item.initialValue = undefined;
      item.itemList = [];
    },
    // 获取省市
    async getRegion() {
      const allRegion = await getRegion();
      this.regionList = this.regionList = allRegion.data.map(x => ({ value: x.regionId, text: x.regionName }));
    },
    // 获取省
    async getProvine() {
      const res = await getProvince();
      this.topFilterList.find(item => item.fieldName === 'province').itemList = res.data.map(x => ({ value: x.regionId, text: x.regionName }));
    },
    // 获取市
    async provinceChange(regionId) {
      this.clearFormItem(this.topFilterList.find(item => item.fieldName === 'city'));
      this.clearFormItem(this.topFilterList.find(item => item.fieldName === 'district'));
      if (!regionId) return;
      const res = await getCitys(regionId);
      this.topFilterList.find(item => item.fieldName === 'city').itemList = res.data.map(x => ({ value: x.regionId, text: x.regionName }));
    },
    // 获取区县
    async cityChange(regionId) {
      this.clearFormItem(this.topFilterList.find(item => item.fieldName === 'district'));
      if (!regionId) return;
      const res = await getDistrict(regionId);
      this.topFilterList.find(item => item.fieldName === 'district').itemList = res.data.map(x => ({ value: x.regionId, text: x.regionName }));
    },
    // 操作员、录入人
    async getAllUser() {
      const res = await getAllUser();
      if (res.resultCode === 200) {
        let userListDict = res.data.map(x => ({ value: x.userId, text: x.employeeName }));
        let targetTableItems = [findTableItem(this.columns, 'operator')];
        targetTableItems.forEach(item => {
          item.dictItems = userListDict;
        });
      }
    },
    // 下载
    async ownerExportData() {
      await getKeyInfoChangeExport(this.fetchParams).then(res => {
        if (res.status !== 'failure') {
          exportExcelFile(res.data, '车主关键信息变更查询');
        } else {
          notifyAction('导出失败', 'error');
        }
      });
    },
    tableChangeFun(val) {
      let userIds = val.map(item => item.operator).join(',');
      userIds && this.queryUserInfoByIds(userIds);
    },
    // 根据用户ids获取用户列表
    async queryUserInfoByIds(userIds) {
      let res = await getNameByIds({ userIds });
      if (res.resultCode == 200) {
        let userDicts = res.data.map(item => ({ text: item.employeeName, value: item.employeeId + '' }));
        let tableTarget = findTableItem(this.columns, 'operator');
        tableTarget.dictItems = tableTarget.filterItems = userDicts;
      }
    }
  }
};
</script>
<style lang="less" scoped></style>
